#ifndef _MUTEX_H
#define _MUTEX_H
#include "config.h"


#ifdef __LINUX
#include <pthread.h>
namespace KCore
{
	class Mutex
	{
	public:
		pthread_mutex_t m_t;
		Mutex()
		{
			pthread_mutex_init(&m_t,0);
		}
		~Mutex()
		{
			pthread_mutex_destroy(&m_t);
		}
	};
	class MutexLock
	{
		pthread_mutex_t* m_t;
	public:
		MutexLock(pthread_mutex_t* p)
		{
			m_t = p;
			pthread_mutex_lock(p);
		}

		MutexLock(Mutex& mutex)
		{
			m_t = mutex.m_t;
			EnterCriticalSection(m_t);	
		};

		~MutexLock()
		{
			pthread_mutex_unlock(m_t);
			m_t = NULL;
		}
	};
}
#else
//windows
#include <Windows.h>
namespace KCore
{
	class _KExport Mutex
	{
	public:
		CRITICAL_SECTION m_t;
		Mutex()
		{
			InitializeCriticalSection(&m_t);
		}
		~Mutex()
		{
			DeleteCriticalSection(&m_t);
		}
	};

	class _KExport MutexLock  
	{
		CRITICAL_SECTION* m_t;
	public:
		MutexLock(CRITICAL_SECTION* p)
		{
			m_t = p;
			EnterCriticalSection(p);	
		};

		MutexLock(Mutex& mutex)
		{
			m_t = &mutex.m_t;
			EnterCriticalSection(m_t);	
		};

		virtual ~MutexLock()
		{
			LeaveCriticalSection(m_t);	
			m_t = NULL;
		}
	};
};

#endif


#endif